import { Component } from '@angular/core';
import {App, IonicPage, NavController, NavParams, LoadingController} from 'ionic-angular';
import {
  ContractApiService, ContractListRequest, RentbillRequest, Rentbill,
  MyEarnestList
} from "../../api/contract-api.service";
import {CheckStandModel} from "../../model/checkstand.model";
import {ROUTER} from "../../service/router.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {USER} from "../../service/user.service";
import {EventService} from "../../service/event.service";
import {PageParam} from "../../api/api.service";
import {TopTabModel} from "../../model/topTab.model";
import {EarnestModel} from "../../model/earnest.model";
import {HouseModel} from "../../model/houseInfo.model";
import {APARTMENTAPISERVICE} from "../../api/apartment-api.service";

/**
 * Generated class for the MyRentingHousePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-my-renting-house',
  templateUrl: 'my-renting-house.html',
  providers:[ContractApiService]
})
export class MyRentingHousePage {
  Rentbills:RentbillRequest;
  Rentbill:Rentbill;
  pageParam:PageParam;
  hasMore:boolean = true;
  ContractList:ContractListRequest[]=[];
  earnestList:Array<EarnestModel>=[];
  items:Array<TopTabModel> = [];
  currentPageIndex:number = 0;
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public app:App,
              public APARTMENTAPISERVICE:APARTMENTAPISERVICE,
              public contractApiService:ContractApiService,
              public eventService:EventService,
              private ls:LocalStorage,
              private ROUTER: ROUTER,
              private USER: USER) {
    this.items.push(new TopTabModel({id:0,name:'我的租房',isSelect:true,func:this.tabChange}));
    this.items.push(new TopTabModel({id:1,name:'我的预订',func:this.tabChange}));
  }
  tabChange = v =>{
    for(let item of this.items){
      item.isSelect=false
    }
    v.isSelect = true;
    this.currentPageIndex = v.id;
  }
  ionViewDidLoad() {
    this.loadMyEarnest();
    this.doRefresh();
    this.eventService.subscribe(EventService.REFRESH_USER,()=>{
      this.doRefresh();
    })

  }
  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.contractApiService.getContractList(v => {
      this.ContractList = v.content
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    },this.pageParam);
    this.loadMyEarnest()
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.contractApiService.getContractList(v => {
      this.hasMore = !v.last;
      this.ContractList = this.ContractList.concat(v.content);
      infiniteScroll.complete();
    },this.pageParam);
  }
  gotoPayRent(v) {
    this.contractApiService.getRentbill({
      contractId:v.id,
      type: 1,
      status:1
    }, v => {
      if(v.content.length >0){
        let tmp = v.content[0]
        let checkstand = new CheckStandModel(tmp.uuid, tmp.content, tmp.money, tmp.contractId,ROUTER.RENTDETAIL)
        this.ls.setObject('checkstand', checkstand);
        this.ROUTER.go(ROUTER.CHECK);
      }else{
        this.USER.toast("该合同已经不在需要缴纳房租")
      }
    })
  }

  gotoRentingDetail(v) {
    this.ROUTER.go(ROUTER.RENTDETAIL, v.id);
  }

  roomStyle(v:any,judge){
    if(judge == "warp") {
      return (v.status==2 || v.status==8 || v.status==4) ? 'sign' : 'no-sign';
    }
    else if(judge == "out-circle"){
      return (v.status==2 || v.status==8 || v.status==4) ? "red-circle" : "gray-circle"
    }
    else if(judge == "dateColor"){
      return (v.status==2 || v.status==8 || v.status==4) ? "red-date" : "gray-date"
    }
  }

  goCheckstand(v) {
    console.log('my-renting-house',v);
    let checkstand = new CheckStandModel(v.billUuid,v.billContent,v.money,v.id,ROUTER.RENTDETAIL);
    this.ls.setObject('checkstand',checkstand);
    this.ROUTER.go(ROUTER.CHECK);
  }

  loadMyEarnest(){
    this.contractApiService.getMyEarnest({},(res:any)=>{
      // this.earnestList = res.content;
      console.log(res);
      let list:Array<EarnestModel> = [];
      for(let item of res.content){
        let data:any=new EarnestModel(item);
        list.push(data);
      }
      this.earnestList=list;
      console.log(this.earnestList);
    })
  }

  goContractPay(item:any){
    console.log(item);
    this.APARTMENTAPISERVICE.getHouseById(item.houseId,v=>{
      this.ls.setObject('houseInfo',new HouseModel(v));
      this.ROUTER.go(ROUTER.CONTRACTPAY)
    })

  }

  ionViewDidEnter() {
    document.title='我的租房';
  }

}
